<?php
	if(!MB_admin){
		exit();
	}
	$kw= $_REQUEST['k'];
	if($kw!=''){
		$param = array(
			'n'=>$kw
		);
		try{
			$srets = $api->getUserInfo($param);
		}catch(MBException $e){
			$srets = array('ret'=>4);	
		}
	}

	$groupUser = new MBAdminGroup($host,$root,$pwd,$db);
   $gid = empty($_GET['gid']) ?  0:(int)$_GET['gid'];
    $groups = $groupUser->showGroup(true);

 
	$rets = $groupUser->getUsersInGroup($gid);
	$thishost = $_SERVER['HTTP_HOST'];
?>
<div class="mainB">
	<form id="modForm" name="modform" method="post" action="" class="table_header">
		<div class="mods">
			<label>用户组：</label>
				<select name="group_name">
                <option value=0>请选择用户组</option>
                <?php foreach($groups['list'] AS $v):?>
					<option value="<?=$v['gid']?>" <?if($gid==$v['gid']):?>selected="selected"<?endif?>><?=$v['gname']?></option>
                <?php endforeach;?>
				</select>
		</div>
		<div><cite>改变下拉菜单可以选择您要的分组</cite></div>
	</form>
	<div class="sline"></div>
	<form id="form2" name="form2" method="post" action="" class="table_header" >
	<label>搜索用户：</label><input type="text" name="k" value="<?php echo htmlencode($kw);?>" style="width:340px;" class="txt" id="rUserk" maxlength="30" /><input type="submit" value="搜索" class="buttonA"/>
		<div><cite>请输入要推荐的用户帐号，选择搜索结果列表中的用户进行推荐</cite></div>
	</form>
	<div class="sline"></div>
	<?if($srets['ret']==0 && $kw != ''):?>
		<div class="table_header"><strong>搜索结果</strong></div>
		<table width="96%" border="0" cellspacing="0" cellpadding="0" align="center" class="tableA">
		  <tr>
			<th>帐号</th>
			<th>姓名</th>
			<th width="100">性别</th>
			<th width="100">消息数</th>
			<th width="120">操作</th>
		  </tr>
		<tr>
			<td id="sel_uname"><?php echo htmlencode($srets['data']['name']);?></td>
			<td><?php echo htmlencode($srets['data']['nick']);?></td>
			<td><? if($srets['data']['sex']==1){echo '男';}elseif($srets['data']['sex']==2){echo '女';}else{echo '未知';}?></td>
			<td><?php echo $srets['data']['tweetnum'];?></td>
			<td><a href="/index.php?m=guest&u=<?php echo htmlencode($srets['data']['name']);?>" target="blank">查看用户</a> <a href="javascript:void(0)" id="newModule" rel="fel">推荐用户</a></td>
		</tr>
		</table>
		<div class="sline"></div>
	<?elseif($kw != ''):?>
		<div class="nodata">未搜索到对应用户，建议变更关键字重新搜索</div>
		<div class="sline"></div>
	<?endif?>
	<div class="table_header"><strong>添加用户</strong></div>
<?if($rets['count']>0):?>	
	<table width="96%" border="0" cellspacing="0" cellpadding="0" align="center" class="tableA">
	  <tr>
		<th width="100">帐号</th>
		<th>微博地址</th>
		<th>分组</th>
		<th>顺序</th>
		<th width="80">添加时间</th>
		<th width="100">操作</th>
	  </tr>
	  <?foreach($rets['list'] as $key => $b):?>
	  <tr>
		<td><?php echo htmlencode($b['u_name']);?></td>
		<td><a href="http://<?php echo $thishost;?>/index.php?m=guest&u=<?php echo htmlencode($b['u_name']);?>" target="_blank">http://<?php echo $thishost;?>/index.php?m=guest&u=<?php echo htmlencode($b['u_name']);?></a></td>
		<td><?php echo $groups['list'][$b['g_id']]['gname'];?></td>
		<td><?php echo intval($b['u_priority']);?></td>
		<td><?php echo date('Y-m-d', $b['u_add_time']);?></td>
		<td><a href="javascript:void(0);" rel="edit" rev="<?php echo $key;?>">编辑</a> <a href="javascript:void(0);" rel="del" rev="<?php echo $b['rid'];?>">删除</a></td>
	  </tr>
	  <?endforeach?>
	</table>
	<?php
		$pages = new MBPage('admin_group.php?cid='.$cindex,$rets['count'],$p,10);
		$pages->showpage();
	?>
<?else:?>
	<div class="nodata">还未添加本分组用户。</div>
<?endif?>
</div>
<script type="text/javascript" >
	var _mlength = <?php echo count($rets['list']);?>;

$('select[name="group_name"]').change(function(){
    window.location.href = 'admin_group.php?cid=1&gid='+$(this).val()+'&k='+$('input[name="k"]').val()
})
function delobj(i){
	$.post('admin_group_act.php?a=gu&m=del',{id:i},function(d){
		eval('r='+d);
		if(r){
			cwin.alert('操作成功！');
		}else{
			cwin.alert('操作成功！');
		}
	});
}
	$('#rUserk').keyup(function(){
		var _t = $(this).val();	
		if(_t.length>30){
			$(this).val(_t.substring(0,30));	
		}
	});



	var _data = [];
	<?if($rets['count']>0):?>	
		<?foreach($rets['list'] as $key => $b):?>
		_data.push({
			'id': '<?php echo $b['rid'];?>',
			'g_id': '<?php echo $b['g_id'];?>',
			'u_name': '<?php echo $b['u_name'];?>',
			'u_priority': '<?php echo intval($b['u_priority']);?>',
			'u_info': '<?php echo htmlencode($b['u_info']);?>'
		});
		<?endforeach?>		
	<?endif?>	

	$('#form2').validate({
			rules:{
				k:{
					required:true,
					maxlength:30
				}
			},
			messages:{
				k:{
					required:'请填写用户帐号',
					maxlength:'请控制在30字符内'
				}
			}
		});			

	var _html = [
		'<form id="dataForm" name="form1" method="post" action="admin_group_act.php?a=gu&m=add" >',
		'	<ul class="form">',
		'		<li><strong>用户帐号：</strong><span id="sName"></span><input type="hidden" name="uname" id="uName"></li>',
		'		<li><strong>用户组：</strong>',
        '<select name="g_id" id=g_id><option value=0>请选择用户组</option>',
        '<?php foreach($groups['list'] AS $v):?>',
        '<option value="<?=$v['gid']?>" <?if($gid==$v['gid']):?>selected="selected"<?endif?>><?=$v['gname']?></option>',
        '<?php endforeach;?></select></li>',
		'		<li><strong>排序：</strong><input id="priority"  type="text" name="priority" style="width:140px;" class="txt" value="9"/></li>',
		'		<li><p><strong style="vertical-align:top;">用户介绍：</strong><textarea rows="5" name="info" id="uInfo"></textarea></p><p><strong>        </strong><cite style="margin:0">用户介绍最多可输入80个汉字</cite></p><input name="rid" id="rid" value="0" type="hidden" /></li>',		
		'		<li><strong></strong><input type="submit" value="确定" class="button"/> <input type="reset" value="取消" class="button closeBtn1"/></li>',
		'	</ul>',
		'</form>'
	];

	var cwin=new moduleObj();
	cwin.autoResize(420,300);
	cwin.hide();	
	$("#newModule").click(function(){
           var sel_uname = $('#sel_uname')?$('#sel_uname').html():'';
		if($('#dataForm').length == 0){ 
			cwin.show({title:"推荐"+sel_uname,text:_html.join('')});
			$("#dataForm").attr('action','admin_group_act.php?a=gu&m=add');
			$('#sName').html($('#sel_uname').html());	
            $('#uName').val($('#sel_uname').html());	
			$('#priority').val(9);
	    	$('#uInfo').val('');
			$('#dataForm').validate({
					rules:{
						uname:{
							required:true,
							maxlength:20
						},
						g_id:{
							minlength:1
						},
						priority:{
							required:true,
							range:[0,999]
						},
						info:{
                         required:true,
							maxlength:80,
							minlength:5
						}
					},
					messages:{
						uname:{
							required:'请填写用户姓名',
							maxlength:'请控制在20字符内'
						},
                        g_id:{
							minlength:'请选择分组'
						},
						priority:{
							required:'请选择排序0-999',
                            range:'顺序在0-999之间'
						},
						info:{
                            required:'请填简介,5到80字符间',
							maxlength:'请控制在80字符内',
                        	minlength:'最少输入5个字符'
						}
					}
				});			
		}else{
			cwin.show({title:"推荐"+sel_uname,text:_html.join('')});
			$('#dataForm label.error').hide();
			$("#dataForm").attr('action','admin_group_act.php?a=gu&m=add');
			$('#sName').html($('#sel_uname').html());	
            $('#uName').val($('#sel_uname').html());	
			$('#priority').val(9);
	    	$('#uInfo').val('');
		}

	});

	$('a[rel=edit]').click(function(){
		var no = $(this).attr('rev');
		if($('#dataForm').length == 0){
			cwin.show({title:"修改推荐",text:_html.join('')});
			$("#dataForm").attr('action','admin_group_act.php?a=gu&m=edit');
            
			$('#sName').html(_data[no].u_name);	
			$('#priority').val(_data[no].u_priority);
			$('#rid').val(_data[no].id);
			$('#uName').val(_data[no].u_name);	
	    	$('#uInfo').val(_data[no].u_info);
			$('#g_id').val(_data[no].g_id);
			$('#dataForm').validate({
					errorPlacement: function(label, element) {
						if(element.attr('name')=='info'){
							label.appendTo(element.parent().next('p'));
						}
					},
					rules:{
						uname:{
							required:true,
							maxlength:20
						},
						priority:{
							required:true,
							range:[0,999]
						},
						info:{
                         required:true,
							maxlength:80,
							minlength:5
						}
					},
					messages:{
						uname:{
							required:'请填写用户姓名',
							maxlength:'请控制在20字符内'
						},
						priority:{
							required:'请选择排序0-999',
                            range:'顺序在0-999之间'
						},
						info:{
                            required:'请填简介,5到80字符间',
							maxlength:'请控制在80字符内',
                        	minlength:'最少输入5个字符'
						}
					}
				});			
		}else{
			cwin.show({title:"修改推荐"});

			$("#dataForm").attr('action','admin_group_act.php?a=gu&m=edit');
			$('#sName').html(_data[no].u_name);	
			$('#priority').val(_data[no].u_priority);
			$('#rid').val(_data[no].id);
			$('#uName').val(_data[no].u_name);	
	    	$('#uInfo').val(_data[no].u_info);
			$('#g_id').val(_data[no].g_id);
		}
	});
	$('a[rel=del]').click(function(){
		var no = $(this).attr('rev');		
		cwin.config('确定要删除此项?','delobj('+no+')');			
	});			
</script>
